//
//  HZAddressDetailSelectViewController.m
//  app
//
//  Created by 刘华舟 on 15/8/27.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//


#import "HZAddressDetailSelectViewController.h"
#import "HDAddNewComManager.h"
#import "HDAddNewComResult.h"
#import "HDSelectCell.h"
#import "HDSelectModel.h"

#import "HZOrderByerAddressModel.h"

static const CGFloat kCartTotalPanelHeight = 120;

@interface HZAddressDetailSelectViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) HDAddNewComManager * addNewComManager;
@property (nonatomic,strong) HDAddNewComResult * addNewComResult;
@property (nonatomic,strong) UITableView * selectTableView;
@property (nonatomic,strong) UIButton * sendBtn;

@property (nonatomic, strong) NSDictionary* selectedDict;

@end

@implementation HZAddressDetailSelectViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.hidden = NO;
    self.selectTableView = [[UITableView alloc]initWithFrame:CGRectOf(CGPointMake(0, 28),
                                                                      [self.view.ubo fw:UB_WIDTH fh:UB_HEIGHT uh:-kCartTotalPanelHeight oh:+56]) style:UITableViewStyleGrouped];
    
    
    self.selectTableView.nothingRefreshView = [[UIRefreshNotingRefreshView alloc]initWithView:self.selectTableView viewController:self tryAgain:@selector(refreshData)];
    self.selectTableView.backgroundColor = [UIColor whiteColor];
    self.selectTableView.delegate = self;
    self.selectTableView.dataSource = self;
    
    [self.view addSubview:self.selectTableView];
    
    
    self.sendBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_RIGHT ox:-20 fy:UB_TOP],
                                                            [self.ubo ow:40 oh:40])];
    
    [self.sendBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.sendBtn setTitleColor:[XCommon hexStringToColor:kColor_red_text] forState:UIControlStateNormal];
    [self.sendBtn.titleLabel setFont:JKAppGlobleFontSmallTagText];
    self.sendBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self.sendBtn addTarget:self action:@selector(exchangeClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.sendBtn];
    
    
    
    
    self.addNewComManager = [HDAddNewComManager shareManager];
    self.addNewComResult = [[HDAddNewComResult alloc]initWithTarget:self action:@selector(selectResult:)];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    if (self.sign.integerValue == 0) {
        self.title = @"小区";
    }
    else if (self.sign.integerValue == 1){
        self.title = @"组团";
    }
    else if (self.sign.integerValue == 2){
        self.title = @"楼牌号";
    }
    else if (self.sign.integerValue == 3){
        self.title = @"房间号";
    }
    
    
    [self.selectTableView.nothingRefreshView show];
    [self sendSelectResult];
}

-(void)refreshData
{
    if (self.sign.integerValue == 0) {
        [self.addNewComManager getCommunityMessage:self.addNewComResult Params:@""  Sign:self.sign];
    }else
    {
        [self.addNewComManager getCommunityMessage:self.addNewComResult Params:self.cmid  Sign:self.sign];
    }
    
    
}

-(void)sendSelectResult
{
    if (self.sign.integerValue == 0) {
        [self.addNewComManager getCommunityMessage:self.addNewComResult Params:@""  Sign:self.sign];
    }else
    {
        [self.addNewComManager getCommunityMessage:self.addNewComResult Params:self.cmid  Sign:self.sign];
    }
}
-(void)selectResult:(HDAddNewComResult*)rt
{
    [self.selectTableView endRefreshingAll];
    
    if (rt.success) {
        [self.selectTableView.nothingRefreshView remove];
        [self.selectTableView reloadData];
    }
    
}

-(void)exchangeClicked:(UIButton*)sender
{
    if (self.selectedDict && self.selectedDict.count == 2) {
        NSString* name = [self.selectedDict stringForKey:@"name" nilValue:@""];
        NSString* ID = [self.selectedDict stringForKey:@"id" nilValue:@""];
        if (self.sign.integerValue == 0) {
            
            
        }else if (self.sign.integerValue == 1){
            self.model.groupName = name;
            self.model.groupID = ID;
        }else if (self.sign.integerValue == 2){
            self.model.buildingName = name;
            self.model.buildingID = ID;
        }else if (self.sign.integerValue == 3){
            self.model.roomName = name;
            self.model.roomID = ID;
        }
        
        if (self.targetCallerAddressChange) {
            [self.targetCallerAddressChange call:self.model];
        }
    }
    
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - tableView代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.addNewComResult.dataSourceArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"cellIdentifier";
    HDSelectCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[HDSelectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (self.sign.integerValue == 0) {
        HDSelectModel * model = self.addNewComResult.dataSourceArray[indexPath.row];
        [cell cellWithModel:model Row:indexPath.row+1 Sign:self.sign];
    }else if (self.sign.integerValue == 1) {
        HDSelectModel * model = self.addNewComResult.dataSourceArray[indexPath.row];
        [cell cellWithModel:model Row:indexPath.row+1 Sign:self.sign];
    }else if (self.sign.integerValue == 2) {
        HDBulidingModel * model = self.addNewComResult.dataSourceArray[indexPath.row];
        [cell cellWithModel:model Row:indexPath.row+1 Sign:self.sign];
    }else if (self.sign.integerValue == 3) {
        HDHouseModel * model = self.addNewComResult.dataSourceArray[indexPath.row];
        [cell cellWithModel:model Row:indexPath.row+1 Sign:self.sign];
    }
    
    
    cell.backgroundView = nil;
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"checkRow"];
    
    
    NSDictionary * dict = [NSDictionary dictionary];
    HDSelectModel * model = self.addNewComResult.dataSourceArray[indexPath.row];
    
    if (self.sign.integerValue == 0) {
        dict = @{@"community_name":model.community_name,@"community_id":model.community_id};
        [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"community"];
        self.selectedDict = @{@"name":model.community_name,@"id":model.community_id};
    }else if (self.sign.integerValue == 1){
        
        dict = @{@"group_name":model.group_name,@"group_id":model.group_id};
        [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"community_zu"];
         self.selectedDict = @{@"name":model.group_name,@"id":model.group_id};
    }else if (self.sign.integerValue == 2){
        
        dict = @{@"building_name":model.building_name,@"id":model.building_id};
        [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"building"];
        self.selectedDict = @{@"name":model.building_name,@"id":model.building_id};
    }else if (self.sign.integerValue == 3){
        
        dict = @{@"house_name":model.house_name,@"community_id":model.community_id,@"id":model.house_id};
        [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"house"];
        self.selectedDict = @{@"name":model.house_name,@"id":model.house_id};
    }
    
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"savacom"];
    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%d",indexPath.row+1] forKey:@"checkRow"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    [self.selectTableView reloadData];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end